package cn.cmcc.online.smsapi.sdk.queue;

import org.json.JSONObject;

import cn.cmcc.online.smsapi.sdk.Iservice.XyCallBack;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.dex.DexUtil;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.NetUtil;
import cn.cmcc.online.smsapi.sdk.net.NewXyHttpRunnable;
import cn.cmcc.online.smsapi.sdk.util.KeyManager;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;


/*SDK-431  /songzhirong/2016.05.23---start---*/
public class ReportLogQueue extends Thread{

		public static final int NORMAL_LOG = 1;
		public static final int ERROR_LOG = 2;
	    static boolean isRun = false;

	    public synchronized static void runReportLogQueue() {

	        if (!NetUtil.checkAccessNetWork(1)) {
                return;
            }// 需要网络，假如网络都没有直接就不需要检查了
	        
	        if (isRun == false ) {
				isRun = true;
	            // 当队列线程序未运行时,才运行
	        	ReportLogQueue queue = new ReportLogQueue();
	            queue.start();
	        }

	    }
	    
	    @Override
	    public void run() {
	        try {
	            this.setName("xiaoyuan_ReportLogQueue");
	            KeyManager.initAppKey();
	            android.os.Process
	                    .setThreadPriority(SdkTaskQueue.XIAOYUAN_THREA_PRIORITY);
	                
	            sendLog(NORMAL_LOG);
	            sendLog(ERROR_LOG);
	            
	        } catch (Throwable e) {

                LogManager.e(Constant.TAG, "run: ", e);

	        }finally{
	        	 isRun = false;
	        }

	    }
	    
	    private void sendLog(int type){       
	        
	    	String postData = DexUtil.queryLog(type, KeyManager.channel,NetUtil.APPVERSION);	    
	    	sendLog(type, postData);


	    }
	  
    public static void sendLog(final int type, final String postData) {
        try {
            if (StringUtils.isNull(postData) || "[]".equals(postData)) {
                return;
            }
            NetUtil.requestNewTokenIfNeed(null);

            /* SDK-526 huangzhiqiang 20160708 start */
            final String token = NetUtil.getToken();
            /* SDK-526 huangzhiqiang 20160708 end */

            XyCallBack callBack = new XyCallBack() {

                @Override
                public void execute(Object... obj) {
                    try {
                        if (obj == null || obj.length < 1) {
                            return;
                        }
                        String resutCode = obj[0].toString();
                        if (resutCode.equals(NewXyHttpRunnable.ERROR_CODE_TOKEN_FAILED)) {
                            /* SDK-526 huangzhiqiang 20160713 start */
                            NetUtil.requestNewTokenAndPostRequestAgain(NetUtil.URL_LOG_SERVICE, postData,
                                    Constant.FIVE_MINUTES, false, false, true, null, token, this);
                            return;
                            /* SDK-526 huangzhiqiang 20160713 end */
                        }
                        if (!resutCode.equals("0") || obj.length < 2) {
                            return;
                        }
                        String response = obj[1].toString();
                        if (StringUtils.isNull(response)) {
                            return;
                        }
                        JSONObject json = new JSONObject(response);
                        String result = json.optString("result");
                        if (result.equals("0")) {
                            DexUtil.postCallback(type, postData);
                        }
                    } catch (Exception e) {
                        LogManager.e(Constant.TAG, "sendLog=", e);
                    }
                }
            };
            NetUtil.executeNewServiceHttpRequest(NetUtil.URL_LOG_SERVICE, postData, callBack, false, false, true, null);
        } catch (Exception e) {
            LogManager.e(Constant.TAG, "sendLog: ", e);
        }
    }

}
/*SDK-431  /songzhirong/2016.05.23---end---*/

